using System;
using System.Reflection;
using Autofac;
using HelloWindowsPhoneMVC.Services;
using WindowsPhoneMVC;
using Module = Autofac.Module;

namespace HelloWindowsPhoneMVC
{
    public class ApplicationModule : Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            builder
                .RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
                .AssignableTo<Controller>()
                .AsSelf();

            //This looks confusing, but it is for performance, and a real app would have decently named services (and less complex)
            builder
                .RegisterType<A>()
                .As<IA>();
            builder
                .RegisterType<B>()
                .As<IB>();
            builder
                .RegisterType<C>()
                .As<IC>();
            builder
                .RegisterType<D>()
                .As<ID>();
            builder
                .RegisterType<E>()
                .As<IE>();

            builder
                .Register(c=>new A2(c.Resolve<IB2>(), c.Resolve<IC2>()))
                .As<IA2>();
            builder
                .Register(c => new B2(c.Resolve<ID2>()))
                .As<IB2>();
            builder
                .Register(c => new C2(c.Resolve<Func<IA2>>()))
                .As<IC2>();
            builder
                .Register(c => new D2(c.Resolve<IE2>()))
                .As<ID2>();
            builder
                .Register(c => new E2())
                .As<IE2>();

            //Put container registrations here
            base.Load(builder);
        }
    }
}
